<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form id="fo">
        <input type="text" name="username">
        <input type="password" name="pwd">
        <button type="submit">提交</button>
    </form>
</body>

</html>
<script>
    //1. 获取表单元素
    var fo = document.querySelector('#fo');
    fo.addEventListener('submit', function (e) {
        e.preventDefault();
        const fd = new FormData(fo);
        const xhr = new XMLHttpRequest();
        xhr.open('post', 'http://www.liulongbin.top:3006/api/formdata');
        xhr.send(fd);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                console.log(xhr.responseText);
            }
        }
    })
</script>